import request from '/@/utils/request';

export const pageList = (params) => {
    return request({
        url: '/admin/user/page',
        method: 'get',
        params,
    });
};

export const addObj = (obj) => {
    return request({
        url: '/admin/user',
        method: 'post',
        data: obj,
    });
};

export const getObj = (id) => {
    return request({
        url: '/admin/user/details/' + id,
        method: 'get',
    });
};

export const delObj = (ids) => {
    return request({
        url: '/admin/user',
        method: 'delete',
        data: ids,
    });
};

export const putObj = (obj) => {
    return request({
        url: '/admin/user',
        method: 'put',
        data: obj,
    });
};

export function getDetails(obj) {
    return request({
        url: '/admin/user/details',
        method: 'get',
        params: obj,
    });
}

// 更改个人信息
export function editInfo(obj) {
    return request({
        url: '/admin/user/edit',
        method: 'put',
        data: obj,
    });
}

export function password(obj) {
    return request({
        url: '/admin/user/password',
        method: 'put',
        data: obj,
    });
}

export function UnbindingUser(type) {
    return request({
        url: '/admin/user/unbinding',
        method: 'post',
        params: {
            type,
        },
    });
}

export function checkPassword(password) {
    return request({
        url: '/admin/user/check',
        method: 'post',
        params: {
            password,
        },
    });
}

/**
 * 注册用户
 */
export const registerUser = (userInfo) => {
    return request({
        url: '/admin/register/user',
        method: 'post',
        data: userInfo,
    });
};

export function validateUsername(rule, value, callback, isEdit) {
    const flag = new RegExp(/^([a-z\d]+?)$/).test(value);
    if (!flag) {
        callback(new Error('用户名支持小写英文、数字'));
    }

    if (isEdit) {
        return callback();
    }

    getDetails({ username: value }).then((response) => {
        const result = response.data;
        if (result !== null) {
            callback(new Error('用户名已经存在'));
        } else {
            callback();
        }
    });
}

export function validatePhone(rule, value, callback, isEdit) {
    if (isEdit) {
        return callback();
    }
    getDetails({ phone: value }).then((response) => {
        const result = response.data;
        if (result !== null) {
            callback(new Error('手机号已经存在'));
        } else {
            callback();
        }
    });
}
